package org.dromara.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.cloud.MiniCloudApi;
import org.dromara.system.domain.SysMiniAdmin;
import org.dromara.system.domain.SysMiniBanner;
import org.dromara.system.domain.bo.SysMiniAdminBo;
import org.dromara.system.domain.bo.SysMiniBannerBo;
import org.dromara.system.domain.vo.SysMiniAdminVo;
import org.dromara.system.domain.vo.SysMiniBannerVo;
import org.dromara.system.mapper.SysMiniAdminMapper;
import org.dromara.system.mapper.SysMiniBannerMapper;
import org.dromara.system.service.ISysMiniAdminService;
import org.dromara.system.service.ISysMiniBannerService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 小程序Banner Service业务层处理
 *
 * @author ling
 */
@RequiredArgsConstructor
@Service
public class SysMiniBannerServiceImpl implements ISysMiniBannerService {


    private final SysMiniBannerMapper baseMapper;

    @Resource
    MiniCloudApi miniCloudApi;


    /**
     * 查询小程序banner
     */
    @Override
    public SysMiniBannerVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询小程序banner列表
     */
    @Override
    public TableDataInfo<SysMiniBannerVo> queryPageList(SysMiniBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMiniBanner> lqw = buildQueryWrapper(bo);
        Page<SysMiniBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小程序banner列表
     */
    @Override
    public List<SysMiniBannerVo> queryList(SysMiniBannerBo bo) {
        LambdaQueryWrapper<SysMiniBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMiniBanner> buildQueryWrapper(SysMiniBannerBo bo) {
        LambdaQueryWrapper<SysMiniBanner> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), SysMiniBanner::getTitle, bo.getTitle());
        lqw.like(StringUtils.isNotBlank(bo.getAuthor()), SysMiniBanner::getAuthor, bo.getAuthor());
        lqw.orderByDesc(SysMiniBanner::getCreateTime);
        return lqw;
    }

    /**
     * 新增小程序banner
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertByBo(SysMiniBannerBo bo) {
        SysMiniBanner add = MapstructUtils.convert(bo, SysMiniBanner.class);
        int result = baseMapper.insert(add);

//        if(jsonResult == null || jsonResult.getInt("errcode") != 0) {
//            throw new ServiceException(jsonResult.getStr("errmsg"));
//        }

        return result;
    }

    /**
     * 修改小程序banner
     */
    @CacheEvict(cacheNames = CacheNames.SYS_MINI_BANNER, key = "#bo.id")
    @Override
    public Boolean updateByBo(SysMiniBannerBo bo) {
        SysMiniBanner sysMiniBanner = MapstructUtils.convert(bo, SysMiniBanner.class);
        Boolean  result = baseMapper.updateById(sysMiniBanner) > 0;

//        JSONObject jsonResult = miniCloudApi.miniBannerUpdate(sysMiniBanner);
//        if(jsonResult == null || jsonResult.getInt("errcode") != 0) {
//            throw new ServiceException(jsonResult.getStr("errmsg"));
//        }

        return result;
    }

    /**
     * 批量删除小程序管理员
     */
    @CacheEvict(cacheNames = CacheNames.SYS_MINI_BANNER, allEntries = true)
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        Boolean result = baseMapper.deleteBatchIds(ids) > 0;

//        for (Long id : ids) {
//            JSONObject jsonResult = miniCloudApi.miniBannerRemove(id);
//            if(jsonResult == null || jsonResult.getInt("errcode") != 0) {
//                throw new ServiceException(jsonResult.getStr("errmsg"));
//            }
//        }

        return result;
    }

}
